package com.hy.mydesktop.client.component.factory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Record;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Layout;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.CheckBoxGroup;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.hy.mydesktop.client.component.meta.AccordionPanelModelEnum;
import com.hy.mydesktop.client.component.meta.FormPanelModelEnum;
import com.hy.mydesktop.client.component.type.ComponentTypeEnum;
import com.hy.mydesktop.client.rpc.ServiceInvoker;
import com.hy.mydesktop.client.rpc.ServiceInvokerAsync;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.ItemOfAssociateToWindow;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceParameter;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceResult;
import com.hy.mydesktop.shared.rpc.util.ComponentTypeEnumConverter;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;

/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-11-20；时间：上午11:28:02</li>
 * <li>类型名称：ContentPanelFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class ContentPanelFactory {

	public static ContentPanel createContentPanel(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		
		return configureContentPanel(gxtComponentMetaNodeModel);
	}
	
	private static ContentPanel configureContentPanel(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		ContentPanel panel = createDefalutContentPanel(gxtComponentMetaNodeModel);
		panel = AbstractComponetFactory.configureAbstractComponet(panel, gxtComponentMetaNodeModel);
		panel = AbstractLayoutContainerComponetFactory.configureAbstractLayoutContainerComponet(panel, gxtComponentMetaNodeModel);
		panel= ContentPanelFactory.configureAttributesOfContentPanel(panel, gxtComponentMetaNodeModel);

		
		
		//添加到父容器时，设置LayoutData
		if (gxtComponentMetaNodeModel.get(FormPanelModelEnum.LAYOUTDATAINPARENTCONTAINER.name()
				.toLowerCase()) != null) {
			String layoutdatainparentcontainer = (String) gxtComponentMetaNodeModel
					.get(FormPanelModelEnum.LAYOUTDATAINPARENTCONTAINER.name().toLowerCase());
			panel.setData("layoutdatainparentcontainer", layoutdatainparentcontainer);

		}
		 
		GxtComponentMetaNodeModelChildrenItemsFactory.configureChildrenItems(panel,gxtComponentMetaNodeModel);
										
		return panel;
	}
	
	public static ContentPanel configureAttributesOfContentPanel(
			ContentPanel panel,GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		if (gxtComponentMetaNodeModel
				.get(AccordionPanelModelEnum.BUTTONALIGN.name().toLowerCase()) != null) {
			String alin = (String) gxtComponentMetaNodeModel
					.get(AccordionPanelModelEnum.BUTTONALIGN.name()
							.toLowerCase());
			if (alin.equals("CENTER")) {
				panel.setButtonAlign(HorizontalAlignment.CENTER);
			} else if (alin.equals("LEFT")) {
				panel.setButtonAlign(HorizontalAlignment.LEFT);
			}

		} 



		if (gxtComponentMetaNodeModel
				.get(AccordionPanelModelEnum.ICON.name().toLowerCase()) != null) {
			AbstractImagePrototype icon = (AbstractImagePrototype) gxtComponentMetaNodeModel
					.get(AccordionPanelModelEnum.ICON.name()
							.toLowerCase());
			panel.setIcon(icon);
			
		}
		
		
		if (gxtComponentMetaNodeModel
				.get(AccordionPanelModelEnum.COLLAPSIBLE.name().toLowerCase()) != null) {
			Boolean collapsible = (Boolean) gxtComponentMetaNodeModel
					.get(AccordionPanelModelEnum.COLLAPSIBLE.name()
							.toLowerCase());
			panel.setCollapsible(collapsible);

		}
		
		if (gxtComponentMetaNodeModel
				.get(AccordionPanelModelEnum.BODYSTYLENAME.name().toLowerCase()) != null) {
			String style = (String) gxtComponentMetaNodeModel
					.get(AccordionPanelModelEnum.BODYSTYLENAME.name()
							.toLowerCase());
			panel.setBodyStyleName(style);

		}
		
		if (gxtComponentMetaNodeModel
				.get(AccordionPanelModelEnum.ANIMCOLLAPSE.name().toLowerCase()) != null) {
			Boolean animCollapse = (Boolean) gxtComponentMetaNodeModel
					.get(AccordionPanelModelEnum.ANIMCOLLAPSE.name()
							.toLowerCase());
			panel.setAnimCollapse(animCollapse);

		}
		
		//设置标题
		if (gxtComponentMetaNodeModel.get(FormPanelModelEnum.HEADING.name()
				.toLowerCase()) != null) {
			String text = (String) gxtComponentMetaNodeModel
					.get(FormPanelModelEnum.HEADING.name().toLowerCase());
			panel.setHeading(text);

		} 
		
		if (gxtComponentMetaNodeModel
				.get(AccordionPanelModelEnum.HEADERVISIBLE.name().toLowerCase()) != null) {
			String headerVisible =  gxtComponentMetaNodeModel
					.get(AccordionPanelModelEnum.HEADERVISIBLE.name()
							.toLowerCase()).toString();
			if ("true".equals(headerVisible)) {
				
				panel.setHeaderVisible(true);
			} else {

				panel.setHeaderVisible(false);
			}

		}
		
		
		if (gxtComponentMetaNodeModel.get(FormPanelModelEnum.FRAME.name()
				.toLowerCase()) != null) {
			String frame =  gxtComponentMetaNodeModel
					.get(FormPanelModelEnum.FRAME.name().toLowerCase()).toString();
			if ("true".equals(frame)) {
				
				panel.setFrame(true);
			} else {

				panel.setFrame(false);
			}

		}
		
		
		
		
		
		return panel;
	}

	private static ContentPanel createDefalutContentPanel(GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		ContentPanel contentPanel = new ContentPanel();
		contentPanel.setHeading("contentPanel");
		contentPanel.setLayout(new FormLayout());
		//contentPanel.
		return contentPanel;
	}

	
	
	
	
}
